package com.blkan.calculator.history;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import java.util.UUID;

public class HistoryItem implements Parcelable {
    private final String ans;
    private final String input;
    private final String result;
    private final UUID uuid;

    public HistoryItem(String ans, String input, String result) {
        this.ans = ans;
        this.input = input;
        this.result = result;
        uuid = UUID.randomUUID();
    }

    public HistoryItem(String ans, String input, String result, UUID uuid) {
        this.ans = ans;
        this.input = input;
        this.result = result;
        this.uuid = uuid;
    }

    protected HistoryItem(Parcel in) {
        ans = in.readString();
        input = in.readString();
        result = in.readString();
        uuid = (UUID) in.readSerializable();
    }

    public static final Creator<HistoryItem> CREATOR = new Creator<HistoryItem>() {
        @Override
        public HistoryItem createFromParcel(Parcel in) {
            return new HistoryItem(in);
        }

        @Override
        public HistoryItem[] newArray(int size) {
            return new HistoryItem[size];
        }
    };

    public String getAns() {
        return ans;
    }

    public String getInput() {
        return input;
    }

    public String getResult() {
        return result;
    }

    public UUID getUuid() {
        return uuid;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@NonNull Parcel parcel, int i) {
        parcel.writeString(ans);
        parcel.writeString(input);
        parcel.writeString(result);
        parcel.writeSerializable(uuid);
    }

    @Override
    public String toString() {
        String ss = String.format("%s=%s (%s)", input, result, ans);
        return ss;
    }

    public static String getViewString(HistoryItem item)
    {
        String ss;
        if(item.input.contains("Ans"))
            ss = String.format("%s=%s (Ans: %s)", item.input, item.result, item.ans);
        else
            ss = String.format("%s=%s", item.input, item.result);
        return ss;
    }

}
